// This is the main DLL file.
//#include "stdafx.h"

#include <time.h>

#include "global.h"

int GlobalModules::LoadGlobalModules()
{   
    int result = OK;

    if(m_global_handler = LoadLibrary(global_module_path))
    {
        if((HRTInitialize = (pfHRTInitialize)GetProcAddress(m_global_handler, "HRTInitialize")) == NULL) result = ERR;
        if((HRTDeinitialize = (pfHRTDeinitialize)GetProcAddress(m_global_handler, "HRTDeinitialize")) == NULL) result = ERR;
        if((CreateTimer = (pfCreateTimer)GetProcAddress(m_global_handler, "CreateTimer")) == NULL) result = ERR;
        if((CancelTimer = (pfCancelTimer)GetProcAddress(m_global_handler, "CancelTimer")) == NULL) result = ERR;
        if((InitLogMan = (pfInitLogMan)GetProcAddress(m_global_handler, "InitLogMan")) == NULL) result = ERR;
        if((LogRegister = (pfLogRegister)GetProcAddress(m_global_handler, "LogRegister")) == NULL) result = ERR;
        if((LogUnRegister = (pfLogUnRegister)GetProcAddress(m_global_handler, "LogUnRegister")) == NULL) result = ERR;
        if((LogOutput = (pfLogOutput)GetProcAddress(m_global_handler, "LogOutput")) == NULL) result = ERR;
        if((InitBufferPool = (pfInitBufferPool)GetProcAddress(m_global_handler, "InitBufferPool")) == NULL) result = ERR;
        if((DeinitBufferPool = (pfDeinitBufferPool)GetProcAddress(m_global_handler, "DeinitBufferPool")) == NULL) result = ERR;
        if((GetSbuf = (pfGetSbuf)GetProcAddress(m_global_handler, "GetSbuf")) == NULL) result = ERR;
        if((FreeSbuf = (pfFreeSbuf)GetProcAddress(m_global_handler, "FreeSbuf")) == NULL) result = ERR;
        if((GetSbufHeader = (pfGetSbufHeader)GetProcAddress(m_global_handler, "GetSbufHeader")) == NULL) result = ERR;
        if((GetSbufControlPointer = (pfGetSbufControlPointer)GetProcAddress(m_global_handler, "GetSbufControlPointer")) == NULL) result = ERR;
        if((GetSbufHeadersize = (pfGetSbufHeadersize)GetProcAddress(m_global_handler, "GetSbufHeadersize")) == NULL) result = ERR;
        if((GetSbufDatasize = (pfGetSbufDatasize)GetProcAddress(m_global_handler, "GetSbufDatasize")) == NULL) result = ERR;
        if((SetSbufDatasize = (pfSetSbufDatasize)GetProcAddress(m_global_handler, "SetSbufDatasize")) == NULL) result = ERR;
        if((ReBuildSbufHeader = (pfReBuildSbufHeader)GetProcAddress(m_global_handler, "ReBuildSbufHeader")) == NULL) result = ERR;

        return result;
    }
    else
    {
        //fallthrough
    }
    return ERR;
}

int GlobalModules::UnLoadGlobalModules()
{
    FreeLibrary(m_global_handler);
    return OK;
}

int GlobalModules::InitializeGlobalModules()
{
    InitBufferPool();
    HRTInitialize();

    m_global_status_handler =
        CreateFileMapping((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE, 0, sizeof(struct GlobalStatus), L"GlobalStatusMap");

    return OK;
}

int GlobalModules::DeinitializeGlobalModules()
{
    CloseHandle(m_global_status_handler);

    HRTDeinitialize();
    DeinitBufferPool();

    return 0;
}


